In [1]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # resize plots
from scipy import stats
import blueice as bl
from blueice.test_helpers import test_conf
In [2]:
m = bl.Model(test_conf())
m.expected_events()
Out[2]:
In [3]:
q = np.linspace(-5, 5, 100)
plt.plot(q, m.sources[0].pdf(q))
Out[3]:
In [4]:
lf = bl.UnbinnedLogLikelihood(test_conf())
lf.add_shape_parameter('some_multiplier', (0.5, 1, 2))
lf.add_rate_parameter('s0')
lf.prepare()
d = lf.base_model.simulate()
print(len(d), lf.base_model.expected_events())
lf.set_data(d)
In [5]:
for k, m in lf.anchor_models.items():
print(k, m.expected_events(), m.sources[0].events_per_day, m.sources[0].fraction_in_range)
In [6]:
x = np.linspace(0.5, 2, 100)
lf.plot_likelihood_ratio(('s0_rate_multiplier', x), ('some_multiplier', x), vmax=100)
plt.xlim(0.5, 2)
plt.ylim(0.5, 2)
Out[6]:
In [7]:
# Should be flat 0 (since fitting foo_rate_multiplier completely compensates for some_multiplier)
lf.plot_likelihood_ratio(('some_multiplier', x))
# Should be centered at 1 (approximately, depends on len(d)):
lf.plot_likelihood_ratio(('some_multiplier', x), s0_rate_multiplier=1)
# Should be centered at 0.5:
lf.plot_likelihood_ratio(('some_multiplier', x), s0_rate_multiplier=2)
# Should be centered at 2
lf.plot_likelihood_ratio(('some_multiplier', x), s0_rate_multiplier=0.5)
plt.ylim(-1, 5)
Out[7]:
In [8]:
# Should be flat 0....
# Except when len(d) deviates significantly from 1000,
# in which case you'll see it following the light blue/red curve.
# Unlike the rate multiplier, the shape parameter is bounded by the anchors we put in, so it can't take a value
# outside (0.5, 2).
lf.plot_likelihood_ratio(('s0_rate_multiplier', x))
# Should be centered at 1
lf.plot_likelihood_ratio(('s0_rate_multiplier', x), some_multiplier=1)
# Should be centered at 0.5
lf.plot_likelihood_ratio(('s0_rate_multiplier', x), some_multiplier=2)
# Should be centered at 2:
lf.plot_likelihood_ratio(('s0_rate_multiplier', x), some_multiplier=0.5)
plt.xlim(0.5, 2)
plt.ylim(-1, 5)
Out[8]:
In [9]:
# Will be centered at 2, and very steep.
# It would like to go to 10, but can't because of the bounds on the shape param
lf.plot_likelihood_ratio(('some_multiplier', np.linspace(0, 3, 100)), s0_rate_multiplier=0.1)
In [10]:
# Should be exactly the same:
plt.plot(x, [lf(some_multiplier=q) for q in x])
plt.plot(x, [lf(s0_rate_multiplier=q) for q in x])
Out[10]:
In [11]:
w1 = lf.make_objective(some_multiplier=0.6)
w2 = lf.make_objective(s0_rate_multiplier=0.6)
print(w1, "\n", w2)
f1 = w1[0]
f2 = w2[0]
In [12]:
# Should be exactly the same
plt.plot(x, [f1([q]) for q in x])
plt.plot(x, [f2([q]) for q in x])
Out[12]:
In [13]:
q = 0.6
kwargs = dict()
a = lf.bestfit_minuit(some_multiplier=q,
minimize_kwargs=dict(print_level=0),
**kwargs)[0]
b = lf.bestfit_minuit(s0_rate_multiplier=q, **kwargs)[0]
print(a, b, len(d)/lf.base_model.expected_events()/q)